/**
                     GNU GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.
 
 */
package com.pic.text;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.json.JSONObject;

public class Translate {

	private static final String TAG = PicText.class.getSimpleName();

    private static final String ENCODING = "UTF-8";
   // private static final String INTERMEDIATE_LANGUAGE = Language.ENGLISH;
    private static final String URL_STRING = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=";
    //private static final String URL_STRING = "http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit";
    	 
    private static final String TEXT_VAR = "&q=";

    /**
     * Translates text from a given language to another given language using Google Translate
     *
     * @param text The String to translate.
     * @param from The language code to translate from.
     * @param to The language code to translate to.
     * @return The translated String.
     * @throws MalformedURLException
     * @throws IOException
     */
    public String translate(String text, String from, String to) throws Exception {
//        if (Language.isValidLanguagePair(from, to)) {
            return retrieveTranslation(text, from, to);
//        } 
//        else {
//            String intermediary = retrieveTranslation(text, from, INTERMEDIATE_LANGUAGE);
//            String result = retrieveTranslation(intermediary, INTERMEDIATE_LANGUAGE, to);
//            return (text.equals(intermediary) || intermediary.equals(result)) ? text : result;
//        }
    }
    /**
     * Forms an HTTP request and parses the response for a translation.
     *
     * @param text The String to translate.
     * @param from The language code to translate from.
     * @param to The language code to translate to.
     * @return The translated String.
     * @throws Exception
     */
    private static String retrieveTranslation(String text, String from, String to) throws Exception {
        try {
            StringBuilder url = new StringBuilder();
            url.append(URL_STRING).append(from).append("%7C").append(to);
            url.append(TEXT_VAR).append(URLEncoder.encode(text, ENCODING));

            Log.i(TAG, "Connecting to " + url.toString());
            HttpURLConnection uc = (HttpURLConnection) new URL(url.toString()).openConnection();
            uc.setDoInput(true);
            uc.setDoOutput(true);
            try {
                Log.i(TAG, "getInputStream()");
                InputStream is= uc.getInputStream();
                String result = toString(is);
                
                JSONObject json = new JSONObject(result);
                String output; 
                output = ((JSONObject)json.get("responseData")).getString("translatedText");
                Log.i(TAG, "HELLO ==  " + output);
                return output; 
            } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html
                uc.getInputStream().close();
                if (uc.getErrorStream() != null) uc.getErrorStream().close();
            }
        } catch (Exception ex) {
            throw ex;
        }
    }

    /**
     * Reads an InputStream and returns its contents as a String. Also effects rate control.
     * @param inputStream The InputStream to read from.
     * @return The contents of the InputStream as a String.
     * @throws Exception
     */
    private static String toString(InputStream inputStream) throws Exception {
        StringBuilder outputBuilder = new StringBuilder();
        try {
            String string;
            if (inputStream != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, ENCODING));
                while (null != (string = reader.readLine())) {
                    outputBuilder.append(string).append('\n');
                }
            }
        } catch (Exception ex) {
            throw new Exception("[google-api-translate-java] Error reading translation stream.", ex);
        }
        return outputBuilder.toString();
    }
}

